<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">

      <el-form-item label="产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="生产工单编码" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入生产工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库单编码" prop="recptCode">
        <el-input
          v-model="queryParams.recptCode"
          placeholder="请输入入库单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <el-card style="border-radius: 10px;margin-top: 15px">
    <el-table border v-loading="loading" :data="lineList">
      <el-table-column label="工单编码" align="center" prop="workorderCode" >
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="handleBomView(scope.row)">{{ scope.row.workorderCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="入库单编码" align="center" prop="recptCode" />
      <el-table-column label="产品编码" align="center" prop="itemCode" />
      <el-table-column label="产品名称" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <el-table-column label="入库数量" align="center" prop="quantityRecived" />
      <el-table-column label="直接材料成本" align="center" prop="directMaterials" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)">{{ scope.row.directMaterials }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="人工成本" align="center" prop="pieceRateWages" />
      <el-table-column label="成本金额" align="center" prop="pieceRateWages"  >
        <template v-slot="scope">
          {{scope.row.directMaterials+scope.row.pieceRateWages}}
        </template>
      </el-table-column>
      <el-table-column label="成本单价" align="center" prop="pieceRateWages"  >
        <template v-slot="scope">
          {{((scope.row.directMaterials+scope.row.pieceRateWages)/scope.row.quantityRecived).toFixed(2)}}
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-table :data="detailList">
        <el-table-column label="物料编码" align="center" prop="itemCode" />
        <el-table-column label="物料名称" align="center" prop="itemName" />
        <el-table-column label="规格型号" align="center" prop="specification" />
        <el-table-column label="单位" align="center" prop="unitOfMeasure" />
        <el-table-column label="数量" align="center" prop="totalQuantity" />
        <el-table-column label="金额" align="center" prop="totalAmount" />
      </el-table>
    </el-dialog>
    <el-dialog :title="title2" :visible.sync="open2" width="70%" height="95%" append-to-body>
        <div id="echartsGx"></div>
    </el-dialog>
  </div>
</template>

<script>
import echarts from "echarts"
import $ from 'jquery'
import request from '@/utils/request'
import { listLine, listDetail,listTree } from "@/api/mes/financial/line";
import Template from "@/views/print/printtemplate/list.vue";

export default {
  name: "Line",
  components: {Template},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成本分配产品表格数据
      lineList: [],
      detailList:[],
      // 弹出层标题
      title: "",
      title2:"关系图",
      open2:false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCode: null,itemName: null, workorderCode: null      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
  },

  methods: {
    /** 查询成本分配产品列表 */
    getList() {
      this.loading = true;
      listLine(this.queryParams).then(response => {
        this.lineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleView(row) {
      listDetail({"lineId":row.lineId}).then(response => {
        this.detailList = response.rows;
        this.open=true;
        this.title = "查看直接成本详情";
      });

    },
    handleBomView(row) {
      listTree(row.workorderCode).then(response => {
        let data=this.handleTree(response.data, 'bomId', 'parentId');
        this.open2=true;
        this.title2 = "BOM成本";
        this.$nextTick(()=>{
          this.echartsGxInit(data);
        })
      });

    },
    echartsGxInit(data){
      let _that = this;
      let chartDom = document.getElementById("echartsGx");
      let myChart = echarts.init(chartDom);
      var colors = [
			"#00ADD0",
			"#FFA12F",
			"#B62AFF",
			"#604BFF",
			"#6E35FF",
			"#002AFF",
			"#20C0F4",
			"#95F300",
			"#04FDB8",
			"#AF5AFF"
		]
		var getdata = function (data) {
			let arr = []
			//
			arr = handle(data, 0)
			return arr;
		}
		var handle = function (data, index, color = '#00f6ff') {
			return data.map((item, index2) => {
        item.name=item.itemName;
        if (index === 0) {
          color = "#FF0000";
          item.itemStyle = {
            color: color,
            borderColor: '#ffffff',
            borderWidth: 2
          };
        }else if (index == 1) {
					color = colors.find((item, eq) => eq == index2 % 10);
				}
				if (index === 0 || index === 1) {
					item.label = {
						position: "inside",
						//   rotate: 0,
						//   borderRadius: "50%",
					}
				}
				// 设置label大小
				switch (index) {
					case 0:
						item.symbolSize = 70
						break;
					case 1:
						item.symbolSize = 50
						break;
					default:
						item.symbolSize = 10
						break;
				}

				item.lineStyle = { color: color }
				if (item.children) {
					item.itemStyle = {
						borderColor: color,
						color: color
					};
					item.children = handle(item.children, index + 1, color)
				} else {
					item.itemStyle = {
						color: 'transparent',
						borderColor: color
					};
				}
				return item
			})
		}

		var option = {
			type: "tree",
			backgroundColor: "#000",
			toolbox: {
				show: true,
				iconStyle: {
					borderColor: "#03ceda"
				},
				feature: {
					restore: {}
				}
			},
			tooltip: {
				trigger: "item",
				triggerOn: "mousemove",
				backgroundColor: "rgba(1,70,86,1)",
				borderColor: "rgba(0,246,255,1)",
				borderWidth: 0.5,
				textStyle: {
					fontSize: 10
				},
        formatter: function (params) {
          if (params.data.children && params.data.children.length > 0) {
            return `直接成本：${params.data.directMaterials}元<br/>人工成本：${params.data.pieceRateWages}元`;
          }
        }
			},
			series: [
				{
					type: "tree",
					hoverAnimation: true,
					data: getdata(data),
					top: 0,
					bottom: 0,
					left: 0,
					right: 0,
					layout: "radial",
					symbol: "circle",
					symbolSize: 10,
					nodePadding: 20,
					animationDurationUpdate: 750,
					expandAndCollapse: true,
					initialTreeDepth: 2,
					roam: true,
					focusNodeAdjacency: true,
					itemStyle: {
						borderWidth: 1,
					},
					label: {
						color: "#fff",
						fontSize: 10,
						fontFamily: "SourceHanSansCN",
						position: "inside",
						rotate: 0,
            overflow: "truncate",
            width: "80%",
            ellipsis: "...",
            formatter: function(params) {
              return params.name.replace(/(.{4})/g, "$1\n");
            }
					},
					lineStyle: {
						width: 1,
						curveness: 0.5,
					}
				}
			]
		};


      option && myChart.setOption(option);

      myChart.hideLoading();

      $(document).ready(function() {
        myChart.resize();
      });
      window.addEventListener("resize", function() {
        myChart.resize();
      });

     /* if(ele == "echartsCphgl"){
        myChart.on('mouseover', function (params) {
            params.componentType != "markLine"?_that.qxzbInit(otherData[params.dataIndex]):1;
        });
      }*/
    },
    searchSpotBg(type) {
        return type === 1
            ? 'linear-gradient(-90deg, #0168CE, #27AFE0)'
            : type === 2
            ? 'linear-gradient(-90deg, #008C4F, #44F093)'
            : 'linear-gradient(270deg, #CB6000, #FFAB1A)';
    }
  }
};
</script>

<style lang="scss" scoped>
#echartsGx{
  width: 100%;
  min-height: 650px;
  background: #000;
}
</style>
